Node.js - require ( )


Posted by mijouhsieh on 2023-04-09

對 Node.js 來說:

  • 如果沒有提供檔案路徑, Node.js 讀取它時就會預設是模組。
  • 如果有包括路徑,那 Node.js 就知道這是個檔案。

  • 而對於副檔名,Node.js 是這樣去判斷的:
    不寫副檔名的時候,預設 require 會先把它當成 .js 處理
    如果不是 .js 則會當作 .json 處理
    如果不是 .json,就會當作 .node 處理
    如果都沒辦法讀取,程式就會炸掉

// require packages and file used in the project
const exphbs = require('express-handlebars') //模組
const movieList = require('./movies.json') //檔案

const movieList = require('./movies') //也可簡化,最好寫完整
關於 require 模組與檔案的語法,可以參考 Node 的官方文件


#node.js #require( )







Related Posts

JQ總務處|如何讓w3schools include HTML能夠讀到JavaScript

JQ總務處|如何讓w3schools include HTML能夠讀到JavaScript

簡單入門 Grid 屬性/用法/使用情境

簡單入門 Grid 屬性/用法/使用情境

[ week10 ] 綜合能力測驗-攻略與解題心得

[ week10 ] 綜合能力測驗-攻略與解題心得


Comments